package flux;

import java.util.Vector;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import sagex.UIContext;
import sagex.api.Configuration;
import sagex.api.Global;

public class utility {
	

	// For making arrays out of properties items with ";name;" separation. Great when you want to use a property
	// item as an array. Has,Get,Set and remove calls.
	
	public static boolean HasPropertyElement(String PropertyName,String DefaultValue,String Element)
	{
				if (Configuration.GetProperty(PropertyName, DefaultValue).contains(";"+ Element + ";"))
				{return true;}
				else
				{return false;}
	}
	
	public static String  SetPropertyElement(String PropertyName,String DefaultValue,String Element)
	{
		    String CurrentElements = Configuration.GetProperty(PropertyName, DefaultValue);
			String result = null;
		    if (CurrentElements.contains(";" + Element + ";"))
			{
		    	result = "0";
								
			}
			else{
				
				String NewElements = CurrentElements + ";" + Element + ";";
				Configuration.SetProperty(PropertyName,NewElements);	
				result = "1";
			}
		    return result;
			}
	
	public static String RemovePropertyElement(String PropertyName,String DefaultValue,String Element)
			{
					String CurrElements = Configuration.GetProperty(PropertyName, DefaultValue);
					String ElementRemoved = null;
					String result = null;
			if (CurrElements.contains(";" + Element + ";"))
			{
													
					ElementRemoved = CurrElements.replaceAll(";"+Element+";","");
															
					Configuration.SetProperty(PropertyName, ElementRemoved);	
					result = "1";							
			   }
			else {
			       result = "0";
			}
				return result;
			
				}
	
	public static String[] GetPropertyArray(String PropertyName,String DefaultValue)
	     {
	          String [] ElementArray = null;
	          String mediaElements = Configuration.GetProperty(PropertyName, DefaultValue);
	          if (mediaElements != null){
	              ElementArray = mediaElements.split(";*;");
	          } else {
	        	  ElementArray = null;
        	  }
	          
	          return ElementArray;
	     }

	public static Object[] toArray(Object Arr)
	/*
	 * returns an Object Array of a passed in Vector, Array, List 
	 * (converts to type Object[])
	 * 
	 * @param Array Array, List, Vector, or Map
	 * -in case of Map returns an array of the KeySet
	 */
	{
		try{
			if (Arr.getClass().isArray()){
				return (Object[])Arr ;
			} else if (Arr instanceof Vector){
				return ((Vector) Arr).toArray();
			} else if (Arr instanceof List){
				return ((List) Arr).toArray();
			}else if (Arr instanceof Map){
				return (((Map) Arr).keySet()).toArray();
			} else {
				System.out.println("toArray(): Unknown Array Type");
				return null;
			}
		} catch (Exception e){
			System.out.println("toArray(): Failed!");
			return null;
		}
	}
	
	public static Object[] AddArrayElements(Object Arr, int i1, int i2)
	/*
	 * Adds blank (null) elements to beginning and end of a passed array.
	 * Good for creating blank "filler" items for tables and the like.
	 * 
	 * @param Arr - Sage MediaFiles, Airings, Shows or Maps in a Vector, list, map, or Array
	 * @param i1 - the number of blank elements to prepad Arr with. 0 for none.
	 * @param i2 - the number of blank elements to append to Arr. 0 for none.
	 */
	{
		Object[] Arr0 = utility.toArray(Arr);
		int length = Arr0.length;
		Object[] Arr1 = new Object[length+i1+i2];
		
		System.arraycopy(Arr0, 0, Arr1, i1, length);
		
		return Arr1;
	}
	
	public static double GetVerticalPositionWithOverscan(String UIContextStr, double A, boolean wrt0)
	/*
	 * Given a position relative to the full visible screen will return the position taking into account overscan.
	 * Good for "Background Components" that need to line up with non background panel widgets.
	 * 
	 * @parmUIContextStr - the result of GetUIContextName() in the STV code.
	 *    Note: When calling from the STV code, use GetUIContextName() directly in the function call
	 * @param A - the UI position (0.00 - 1.00)
	 * @param wrt0 - true = A is wrt 0 (top), false = A is wrt 1 (bottom) 
	 */
	{
		double B = 0;
		
		if (A < 0){
			A = 0;}
		if (A>1.0){
			A = 1.0;}
		
				
		double Dim = Global.GetFullUIHeight(new UIContext(UIContextStr));
		double Offset = Configuration.GetOverscanOffsetY(new UIContext(UIContextStr));
		double Scale = Configuration.GetOverscanScaleHeight(new UIContext(UIContextStr));

		if (wrt0){
			B = Offset/Dim + A*Scale;
		}else{
			B = 1 - (Offset/Dim + (1-A)*Scale);
		}
		
		if (B < 0){
			B = 0;}
		if (B>1.0){
			B = 1.0;}
	
		//System.out.println("Vertical Position With Overscan");
		//System.out.println("A = '" + A + "'");
		//System.out.println("FullUIWidth = '" + Dim + "'");
		//System.out.println("OverscanOffsetX = '" + Offset + "'");
		//System.out.println("OverscanScaleWidth = '" + Scale + "'");
		//System.out.println("B = '" + B + "'");
		
		return B;
	}
	
	public static double GetHorizontalPositionWithOverscan(String UIContextStr, double A, boolean wrt0)
	/*
	 * Given a relative position will return the true position taking into account overscan.
	 * Good for "Background Components" that need to line up with non background panel widgets.
	 * 
	 * @parmUIContextStr - the result of GetUIContextName() in the STV code.
	 *    Note: When calling from the STV code, use GetUIContextName() directly in the function call
	 * @param A - the UI position (0.00 - 1.00)
	 * @param wrt0 - true = A is wrt 0 (left), false = A is wrt 1 (right) 
	 */
	{
		double B = 0;
		
		if (A < 0){
			A = 0;}
		if (A>1.0){
			A = 1.0;}
		
		double Dim = Global.GetFullUIWidth(new UIContext(UIContextStr));
		double Offset = Configuration.GetOverscanOffsetX(new UIContext(UIContextStr));
		double Scale = Configuration.GetOverscanScaleWidth(new UIContext(UIContextStr));
	
		if (wrt0){
			B = Offset/Dim + A*Scale;
		}else{
			B = 1 - (Offset/Dim + (1-A)*Scale);
		}
		
		if (B < 0){
			B = 0;}
		if (B>1.0){
			B = 1.0;}
		
		//System.out.println("Horizontal Pos With Overscan");
		//System.out.println("A = '" + A + "'");
		//System.out.println("FullUIWidth = '" + Dim + "'");
		//System.out.println("OverscanOffsetX = '" + Offset + "'");
		//System.out.println("OverscanScaleWidth = '" + Scale + "'");
		//System.out.println("B = '" + B + "'");
		
		return B;
	}
}